/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.generators.reports;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bugstat.common.TimeInterval;
import org.bugstat.common.bugs.User;
import org.bugstat.common.reports.Report;
import org.bugstat.generators.ReportGeneratorContext;

/**
 * This is a base class for per-person reports
 *
 * @author Basil Shikin
 */
public abstract class PerPersonReportGeneratorBase<T extends TimedReportBase>
    extends ReportGeneratorBase
{
    protected TimeInterval interval = null;

    protected Map<User, T> reports = new HashMap<User, T>();
    protected int valueThreshold = 10;

    public void setValueThreshold(int valueThreshold)
    {
        this.valueThreshold = valueThreshold;
    }

    @Override
    public void startedGeneratingReports( TimeInterval timeInterval, ReportGeneratorContext context )
    {
        super.startedGeneratingReports(timeInterval, context);
        
        interval = timeInterval;
    }
    
    @Override
    public List<Report> getGeneratedReports()
    {
        List<T> result = new ArrayList<T>();
        result.addAll( reports.values() );
        
        // First, order by max counts
        Collections.sort( result, new Comparator<T>() {
            @Override
            public int compare(T o1, T o2)
            {
                return o2.getMaxValue()- o1.getMaxValue();
            }
            
        });
        
        // Second, remove all people who have less then bugs threshold
        int endIndex = 0;
        while ( endIndex < result.size() )
        {
            if ( result.get( endIndex ).getMaxValue() <= valueThreshold ) break;
            endIndex += 1;
        }
        result = result.subList(0, endIndex);
        
        final List<Report> castResult = new ArrayList<Report>( result.size() );
        castResult.addAll( result );
        
        return castResult;
    }
    
    protected T retrieveReport(User assignedTo)
    {
        T result = reports.get( assignedTo );
        if ( result == null )
        {
            result = createNewReport( assignedTo );
            
            result.setName( assignedTo.getName() );            
            result.setVisibleByDefault( false );
            result.setTimeInterval( interval );
            
            reports.put( assignedTo, result );
        }
        
        return result;
    }

    protected abstract T createNewReport(User assignedTo);
}
